package com.entities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

//import com.fetch.FetchXML;

public class News {
	
	public static final String[] fieldNames = new String[]{"title","link", "pubDate", "author"};
	public static final String tagName = "item";
	
	protected static int getTagPubDate(){
		return 0;
	}
	protected static int getTagTitle() {
		return 1;
	}
	protected static int getTagLink() {
		return 2;
	}
	
	protected static int getTagAuthor() {
		return 3;
	}
	
	public static HashMap<String, List<News>> genNews = new HashMap<String, List<News>>();
	public static HashMap<String, List<News>> youTubeNews = new HashMap<String, List<News>>();
	public News(String mTtitle, String mLink, Date date, String author) {
		super();
		this.mTtitle = mTtitle;
		this.mLink = mLink;
		mDate = date;
		mAuthor = author;
	}

	protected String mTtitle;
	protected String mLink;
	protected Date mDate;
	protected String mAuthor;
	
	public static List<News> getNewsBlog(List<List<String>> fetchedList, String tag) {
		if(genNews.get(tag)==null){
			List<News> newsList = new ArrayList<News>(); 

				for(List<String> l: fetchedList){	
					try {
					SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZZZ", Locale.ENGLISH);
					String stringDate = new String(l.get(2));
					Date date;
					
						date = formatter.parse(stringDate);
				
					String link = new String(l.get(1));

					String title = l.get(0);
					newsList.add(new News(
							title,
							link, date, ""));
					
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}

			genNews.put(tag, newsList);
			} 
		return genNews.get(tag);
	}
	
	public static List<News> getYouTube(List<List<String>> list, String tag){
		
			List<News> newsList = new ArrayList<News>(); 
			try {
				for(List<String> l: list){	
					
					SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZZZ", Locale.ENGLISH);
					String stringDate = new String(l.get(getTagPubDate()));
					Date date = formatter.parse(stringDate);
					String link = new String(l.get(getTagLink()));

					String title = l.get(getTagTitle());
					newsList.add(new News(
							title,
							link, date, l.get(getTagAuthor())));
					}
				
				}  catch (ParseException e) {
					e.printStackTrace();
				}
			youTubeNews.put(tag, newsList);
		return newsList;
	}
	
	public Date getDate() {
		return mDate;
	}

	public String getTtitle() {
		return mTtitle;
	}

	public String getLink() {
		return mLink;
	}
	public String getAuthor() {
		
		return mAuthor;
	}


}
